package andy.timerTask;


import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

public class A_TimerTaskActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	private ListPreference lp;
	private CheckBoxPreference ckpf1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 所的的值将会自动保存到SharePreferences
		addPreferencesFromResource(R.xml.timmer_main_preference);

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		prefs.registerOnSharedPreferenceChangeListener(this);

		lp = (ListPreference) findPreference("depart");
		ckpf1 = (CheckBoxPreference) findPreference("CheckBox1");

	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {

		return super.onPreferenceTreeClick(preferenceScreen, preference);

	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		System.out.println("onSharedPreferenceChanged  " + "key=" + key);
		if ("depart".equalsIgnoreCase(key)) {
			System.out.println("lp.getValue()=" + lp.getValue());
			System.out.println("lp.getEntry()= " + lp.getEntry());

			System.out.println("" + lp.getEntryValues());
			System.out.println("" + lp.getEntries());

			CharSequence[] entryValues = lp.getEntryValues();
			for (int i = 0; i < entryValues.length; i++) {
				System.out.println("entryValues[" + i + "]" + entryValues[i]);
			}

			CharSequence[] entries = lp.getEntries();
			for (int i = 0; i < entries.length; i++) {
				System.out.println("entries[" + i + "]" + entries[i]);
			}

		} else if ("CheckBox1".equalsIgnoreCase(key)) {
			System.out.println("ckpf1.getDisableDependentsState() ="
					+ ckpf1.getDisableDependentsState());
			System.out.println("ckpf1.getOrder() =" + ckpf1.getOrder());
			System.out.println("ckpf1.getSummary() =" + ckpf1.getSummary());
			System.out.println("ckpf1.isChecked() =" + ckpf1.isChecked());
		}

		/**
		 * 第二种监听 因为所有的都继承自Preference Preference pref = findPreference(key); if
		 * (pref instanceof EditTextPreference) { EditTextPreference etp =
		 * (EditTextPreference) pref; if(pref.getKey().equals("password")){
		 * pref.setSummary(etp.getText().replaceAll(".","*")); }else{
		 * pref.setSummary(etp.getText()); } }else if(pref instanceof
		 * CheckBoxPreference){ if(((CheckBoxPreference)pref).isChecked())
		 * pref.setSummary("On"); else pref.setSummary("Off"); }
		 */
	}

}
